import java.awt.BorderLayout;

public class SodiumUI extends JFrame {

	private JPanel contentPane;
	public static String food, time, messages[];
	public int sps = -1, sc = -1;

	public SodiumUI() {
		setTitle("Sodium Nutritionist");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		final TextArea messagesArea = new TextArea();
		messagesArea.setEditable(false);
		messagesArea.setText(Time.getCurrentTime().concat("What did you eat?\n"));
		messagesArea.addTextListener(new TextListener() {
			@Override
			public void textValueChanged(final TextEvent e) {
				messages = messagesArea.getText().split("\n");
				if (Messages.getMessageAt(messages.length - 2).contains("How many servings") || Messages.getMessageAt(messages.length - 2).contains("servings using numbers")) {
					try {
						sc = Messages.getNumber(Messages.getLastMessage().substring(Messages.getLastMessage().indexOf("]") + 2, Messages.getLastMessage().length())) * sps;
					} catch (final NumberFormatException e1) {
						messagesArea.append(Time.getCurrentTime().concat("Please enter a valid amount of servings using numbers.\n"));
					}
					if (sc >= 0) {
						messagesArea.append(Time.getCurrentTime().concat("You consumed ").concat(Integer.toString(sc)).concat(" mg of sodium.\n"));
					}
				} else if (Messages.getMessageAt(messages.length - 2).contains("How much sodium") || Messages.getMessageAt(messages.length - 2).contains("sodium using numbers")) {
					try {
						sps = Messages.getNumber(Messages.getLastMessage().substring(Messages.getLastMessage().indexOf("]") + 2, Messages.getLastMessage().length()));
					} catch (final NumberFormatException e1) {
						messagesArea.append(Time.getCurrentTime().concat("Please enter a valid amount of sodium using numbers.\n"));
					}
					if (sps != -1) {
						if (food.charAt(food.length() - 1) == 's') {
							messagesArea.append(Time.getCurrentTime().concat("How many servings of ").concat(food).concat(" did you eat?\n"));
						} else {
							messagesArea.append(Time.getCurrentTime().concat("How many servings of ").concat(food).concat("s did you eat?\n"));
						}
					}
				} else if (Messages.getMessageAt(messages.length - 2).contains("What did you")) {
					food = Messages.getLastMessage().trim().substring(Messages.getLastMessage().indexOf("]") + 2, Messages.getLastMessage().length()).toLowerCase();
					if (food.charAt(food.length() - 1) == 's') {
						messagesArea.append(Time.getCurrentTime().concat("How much sodium is there in a serving of ").concat(food).concat("?\n"));
					} else {
						messagesArea.append(Time.getCurrentTime().concat("How much sodium is there in a ").concat(food).concat("?\n"));
					}
				}
			}
		});

		contentPane.add(messagesArea, BorderLayout.CENTER);

		final TextField messagesField = new TextField();
		messagesField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(final KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					messagesArea.append(Time.getCurrentTime().concat(messagesField.getText()).concat("\n"));
					messagesField.setText("");
				}
			}
		});
		contentPane.add(messagesField, BorderLayout.SOUTH);
	}
}